package cn.gbase.jiangsu.data.transfer.service;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.SystemUser;
import cn.gbase.jiangsu.data.transfer.util.DefaultPasswordEncoder;

@IocBean(args = { "refer:dao" })
public class SystemUserService extends IdEntityService<SystemUser> {

	public SystemUserService() {
		super();
	}

	public SystemUserService(Dao dao) {
		super(dao);
	}

	/**
	 * 根据ID取得用户.
	 * 
	 * @param userId
	 * @return
	 */
	public SystemUser getUser(int userId) {
		return this.dao().fetch(SystemUser.class, userId);
	}

	/**
	 * 根据用户名取得用户.
	 * 
	 * @param username
	 * @return
	 */
	public SystemUser getUser(String username) {
		username = username.replace("'", ""); // 替换单引号
		return this.dao().fetch(SystemUser.class,
				Cnd.where("username", "=", username));
	}

	/**
	 * 更新密码.
	 * 
	 * @param user
	 * @return
	 */
	public void updatePWS(SystemUser user) {

		// 密码加密
		String encodePass = new DefaultPasswordEncoder("MD5").encode(user
				.getNewPassword());

		// 更新
		this.dao().update(SystemUser.class, Chain.make("pw", encodePass),
				Cnd.where("id", "=", 1));
	}

}
